package com.aibili.contoller.anonymous;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.aibili.pojo.ChargeRecord;
import com.aibili.pojo.MomentComment;
import com.aibili.pojo.MomentInfo;
import com.aibili.pojo.UserAccount;
import com.aibili.pojo.UserAnonymous;
import com.aibili.pojo.UserInfo;
import com.aibili.service.AnonymousService;
import com.aibili.service.GiftService;
import com.aibili.service.MomentService;
import com.aibili.service.UserInfoService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.util.UtilDate;
import com.daoshun.common.CommonUtils;

@Controller
@RequestMapping("/anonymous")
public class AnouymousController {

	@Resource
	private UserInfoService userInfoService;

	@Resource
	private AnonymousService anonymousService;

	@Resource
	private MomentService monentService;
	
	@Resource
	private GiftService giftService;
	/**
	 * 
	 * @Description 通过匿名id 获取匿名用户信息
	 * @param anonymous_id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getanonymoushead", produces = "application/json; charset=utf-8")
	public String getanonymoushead(String anonymous_id) {
		HashMap<String, Object> map = new HashMap<>();

		UserAnonymous anonymous = userInfoService.getAnonymousByAnonid(anonymous_id);
		if (anonymous != null) {
			map.put("anonymous", anonymous);
			map.put("code", 0);
		} else {
			map.put("code", 1);
		}

		return JSON.toJSONString(map);
	}

	/**
	 * 
	 * @Description 匿名用户注册
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/anonymousregister", produces = "application/json; charset=utf-8")
	public String anonymousregister() {
		HashMap<String, Object> map = new HashMap<>();

		// 获得注册的环信id
		String anon_id = anonymousService.getAnonymousid();

		//System.out.println(anon_id);
		// 获得注册的环信密码
		String anon_pass = CommonUtils.getRandomString(10);
		String pass = CommonUtils.MD5(anon_pass);

		// 注册
		String backstr = anonymousService.register(anon_id, pass, "");

		if (!backstr.equals("")) {
			JSONObject backjson = JSONObject.parseObject(backstr);
			String error = backjson.getString("error");
			if (error == null) {
				map.put("my_id", anon_id);
				map.put("anonymous_pass", pass);
				map.put("code", 0);
			} else {
				map.put("code", 1);
				map.put("message", "error");
			}
		}
		return JSON.toJSONString(map);
	}

	@ResponseBody
	@RequestMapping(value = "/share", produces = "application/json; charset=UTF-8")
	public ModelAndView homeuserinfo(String id) {
		ModelAndView mav = new ModelAndView();
		if (!CommonUtils.isEmptyString(id)) {
			long mid = CommonUtils.parseLong(id, 0);
		    MomentInfo momentInfo =	monentService.getMonentInfo(mid);
		    if (momentInfo!=null) {
		    	UserInfo user = momentInfo.getUser();//发布人
		    	List<MomentComment> comments = monentService.getCommentListByMommentNopage(momentInfo.getId());
		    	mav.addObject("user",user);
		    	mav.addObject("comments",comments);
		    }
			mav.addObject("momentInfo",momentInfo);
		}
		return mav;
	}
	
	@ResponseBody
	@RequestMapping(value = "/indexalipay", produces = "application/json; charset=UTF-8")
	public ModelAndView alipay(long id,double money,String ordernum) {
		ModelAndView mav = new ModelAndView();
		
			UserInfo user = userInfoService.getById(id);
			if (user!=null) {
			String name = user.getReal_name();
			Date date = new Date();
			String newdate = UtilDate.getDate();
			double Money = -money;
			UserAccount account = userInfoService.getUserAccountByUserid(user.getId());
			if(account != null){
			String cardnum = account.getCard_num();
		    String detail = ordernum+"^"+cardnum+"^"+name+"^"+Money+"^"+"备注|";//流水号1^收款方帐号1^真实姓名^付款金额1^备注说明1 
		    	mav.addObject("detail",detail);
			}
			mav.addObject("newdate",newdate);
	    	mav.addObject("ordnum",ordernum);
	    	mav.addObject("money",Money);
			}
		return mav;
	}
	
}
